#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"

#include <QtCore/QDebug>

#ifdef Q_WS_X11
#include <QtGui/QX11Info>

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#endif
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QmlApplicationViewer viewer;
//    viewer.addPluginPath(QLatin1String("plugins"));
//    viewer.addPluginPath(QLatin1String("plugins"));
    viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    viewer.setMainQmlFile(QLatin1String("qml/main.qml"));
    viewer.showExpanded();

#if 0
    qDebug() << viewer.effectiveWinId();

    Display *display = QX11Info::display();
    if (!display)
        qDebug("Failed to receive Display");

    bool remove = false;
    int priority = 1;
    Atom atom = XInternAtom(display, "_MEEGO_STACKING_LAYER", False);
    qDebug("Priority %d remove %d atom %d", static_cast<int>(priority), 0, static_cast<int>(atom));

    if (atom == BadAlloc || atom == BadValue)
        qDebug("Failed To generate a vaild atom response");
    if (atom == None)
        qDebug("Atom response is already exist?..");

    qint32 currentValue = 0;
    bool itemExist = true;
    {
        // check the current value

        Atom actualType = Success;
        int actualFormat = 0;
        unsigned long nitems;
        unsigned long bytes_after;
        qint32 *curValPointer = 0;
        if (XGetWindowProperty(display, viewer.effectiveWinId(), atom, 0, 4, False, XA_CARDINAL, &actualType, &actualFormat,
                               &nitems, &bytes_after, (unsigned char **)&curValPointer) == Success)
        {
            if (nitems > 0)
            {
                // item found
                if (curValPointer)
                    currentValue = *curValPointer;
            }
            else
            {
                // item is missing
                itemExist = false;
            }

            XFree((void *)curValPointer);
        }
    }

    qint32 value = static_cast<qint32>(priority);

    qDebug("Current value: %d (exist %d)", currentValue, itemExist);

    // property has to be removed - and it is missing already, do nothing
    if (remove && !itemExist)
        qDebug("Removed already");

    // if the same property already setted up - do nothing again
    if (itemExist && currentValue == value)
        qDebug("Same value is setted up");

    // change or remove property
    if (remove)
        XDeleteProperty(display, viewer.effectiveWinId(), atom);
    else
        XChangeProperty(display, viewer.effectiveWinId(), atom, XA_CARDINAL, 32, PropModeReplace, (unsigned char*)&value, 1);

    qDebug("Property is updated");

    XSync(display, False);
#endif
#ifdef Q_WS_X11
    bool enabled = true;
    QWidget *activeWindow = QApplication::activeWindow();
    if (activeWindow)
    {
        Display *dpy = QX11Info::display();
        Window w = activeWindow->effectiveWinId();

        unsigned long val = (enabled) ? 1 : 0;
        Atom atom = XInternAtom(dpy, "_MEEGOTOUCH_CANNOT_MINIMIZE", false);
        if (atom)
        {
            XChangeProperty (dpy,
                             w,
                             atom,
                             XA_CARDINAL,
                             32,
                             PropModeReplace,
                             reinterpret_cast<unsigned char *>(&val),
                             1);
        }
        else
        {
            qWarning("Unable to obtain _MEEGOTOUCH_CANNOT_MINIMIZE. This example will only work "
                 "with the MeeGo Compositor!");
        }
    }

    //        d->allowSwipe = enabled;
//        emit allowSwipeChanged();
#endif

    return app.exec();
}
